﻿@page "/users1"
@using BootstrapBlazorApp.OnlyServer.Pages.Main.Components
@using BootstrapBlazorApp.OnlyServer.Pages.Main.Users

<PageTitle>用户管理</PageTitle>


<Table TItem="UserDto" ShowToolbar="true" ShowAddButton="false" ShowEditButton="false"
       IsStriped="true" IsBordered="true" ShowExtendButtons="true" ShowExtendEditButton="false" ClickToSelect="true"
       ShowDeleteButton="false" ShowExtendDeleteButton="false" ShowSearch="true" ShowAdvancedSearch="false"
       OnQueryAsync="OnQueryAsync" SearchMode="SearchMode.Top" SearchModel="@Filter"
       IsMultipleSelect="true" OnResetSearchAsync="OnResetSearchAsync"
       OnClickRowCallback="OnClickRowCallback">
    <SearchTemplate>
        <GroupBox Title="搜索条件">
            <BootstrapBlazorApp.OnlyServer.Pages.Main.Components.Port @bind-SelectedValue="@context.Port" />
        </GroupBox>
    </SearchTemplate>
    <TableToolbarTemplate>
        <TableToolbarButton Text="添加" TItem="UserDto" OnClickCallback="OnClickCallback"></TableToolbarButton>
        <TableToolbarButton Text="编辑" TItem="UserDto" OnClickCallback="OnClickEditCallback"></TableToolbarButton>
    </TableToolbarTemplate>
    <TableColumns>
        <TableColumn @bind-Field="@context.Name" Editable="false" />
        <TableColumn @bind-Field="@context.Age" Editable="false" FormatString="D4" />
        <TableColumn @bind-Field="@context.Email" Editable="false" />
        <TableColumn @bind-Field="@context.Port" Editable="false" />
        <TableColumn @bind-Field="@context.Grt" Editable="false" FormatString="N4" />
    </TableColumns>
</Table>

<Modal @ref="AddModal" IsKeyboard="true">
    <ModalDialog Title="默认弹窗">
        <BodyTemplate>
            <ValidateForm @ref="UserValidate" Model="Dto" ValidateAllProperties="true">
                <div class="row g-3 form-inline">
                    <div class="col-12 col-sm-6 col-md-6">
                        <BootstrapInput @bind-Value="Dto.Name" />
                    </div>
                    <div class="col-12 col-sm-6 col-md-6">
                        <BootstrapInputNumber @bind-Value="Dto.Age" />
                    </div>
                    <div class="col-12 col-sm-6 col-md-6">
                        <BootstrapInput @bind-Value="Dto.Email" />
                    </div>
                    <div class="col-12 col-sm-6 col-md-6">
                        <BootstrapBlazorApp.OnlyServer.Pages.Main.Components.Port @bind-Value="Dto.Port" />
                    </div>
                </div><Button ButtonType="ButtonType.Submit" Text="保存" OnClick="OnSaveAsync" />
            </ValidateForm>
        </BodyTemplate>
    </ModalDialog>
</Modal>

@code {
    Modal AddModal;
    public IEnumerable<UserDto> UserItems { get; set; }
    List<UserDto> Items = new();
    public UserCreateOrUpdateDto Dto { get; set; } = new();

    public ValidateForm UserValidate;

    UserDto CurrentItem { get; set; }

    public enum ViewMode
    {
        Add,
        Edit
    }

    public class FilterModel : IFilter
    {
        public string Port { get; set; }
        public IFilterAction FilterAction { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    }

    ViewMode CurrentMode;

    UserDto Filter { get; set; } = new();

    Task<QueryData<UserDto>> OnQueryAsync(QueryPageOptions options)
    {
        if (!string.IsNullOrEmpty(options.SearchText))
        {
            string text = options.SearchText;
            UserItems = Items.Where(x => x.Email.Contains(text) || x.Name.Contains(text));
        }
        else
        {
            UserItems = Items;
        }
        if (!string.IsNullOrEmpty(Filter.Port))
        {
            UserItems = UserItems.Where(x => x.Port == Filter.Port);
        }
        return Task.FromResult(new QueryData<UserDto>()
            {
                Items = UserItems
            });
    }

    Task OnClickRowCallback(UserDto dto)
    {
        CurrentItem = dto;
        return Task.CompletedTask;
    }

    Task OnSaveAsync()
    {
        if (UserValidate.Validate())
        {
            List<UserDto> Items = UserItems.ToList();
            if (CurrentMode == ViewMode.Add)
            {
                Items.Add(new UserDto()
                    {
                        Id = Dto.Id,
                        Age = Dto.Age,
                        Email = Dto.Email,
                        Name = Dto.Name,
                        Port = Dto.Port
                    });
                UserItems = Items;
            }
            else if (CurrentMode == ViewMode.Edit)
            {
                Items.ForEach(x =>
                {
                    if (x.Id == Dto.Id)
                    {
                        x.Port = Dto.Port;
                        x.Age = Dto.Age;
                        x.Email = Dto.Email;
                        x.Name = Dto.Name;
                    }
                });
            }
            AddModal.Close();
        }
        return Task.CompletedTask;
    }

    Task OnClickEditCallback(IEnumerable<UserDto> items)
    {
        if (CurrentItem != null)
        {
            CurrentMode = ViewMode.Edit;
            var dto = CurrentItem;
            Dto = new()
                {
                    Id = dto.Id,
                    Age = dto.Age,
                    Email = dto.Email,
                    Name = dto.Name,
                    Port = dto.Port
                };
            AddModal.Toggle();
            StateHasChanged();
        }
        return Task.CompletedTask;
    }
    Task OnClickCallback(IEnumerable<UserDto> items)
    {
        CurrentMode = ViewMode.Add;
        Dto = new() { Id = Guid.NewGuid() };
        AddModal.Toggle();
        StateHasChanged();
        return Task.CompletedTask;
    }

    protected override void OnInitialized()
    {

        Items.Add(new UserDto()
            {
                Id = Guid.NewGuid(),
                Name = "流川枫",
                Age = 37,
                Email = "lcf@163.com",
                Port = "CN",
                Grt = 3.3m
            });
        Items.Add(new UserDto()
            {
                Id = Guid.NewGuid(),
                Name = "樱木花道",
                Age = 17,
                Email = "92939329@qq.com",
                Port = "CN",
                Grt = 4.2312m
            });
        Items.Add(new UserDto()
            {
                Id = Guid.NewGuid(),
                Name = "三井寿",
                Age = 6,
                Email = "zzh@qq.com",
                Port = "US",
                Grt = 2
            });

        UserItems = Items;
    }

    Task OnValidSubmit(EditContext context)
    {
        List<UserDto> Items = UserItems.ToList();
        Items.Add(new UserDto()
            {
                Id = Dto.Id,
                Age = Dto.Age,
                Email = Dto.Email,
                Name = Dto.Name,
                Port = Dto.Port
            });
        UserItems = Items;
        return Task.CompletedTask;
    }

    Task OnResetSearchAsync(UserDto fitler)
    {
        fitler.Port = "";
        return Task.CompletedTask;
    }
}
